package com.xinhe.web.config;

import com.xinhe.web.interceptor.SessionInterceptor;
import com.xinhe.web.session.UserSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
//public class WebMvcConfg implements WebMvcConfigurer {
public class WebMvcConfg extends WebMvcConfigurationSupport {

    private static final String[] UN_VALIDATE_STR = new String[]{"login", "logout", "register", "enumConstant", "valueConfig", "refundNotify", "ueditor", "test", "weixinUser/sessionKeyAndOpenId", "shorMessage", "validToken", "imageText", "swagger", "file", "upload", "error", "temp", "druid", "favicon", "doc", "webjars", "test", "importMembers", "importMembersWithdrawals", "importMembersAdoptedOrder", "importMembersYunChaSeed", "importMembersConsumerGoldRedEnvelope", "importMembersTwoAndThree", "importMembersTeaOilCardRecord", "weixinUserAttentionNotify", "responseJsapiPay", "findByObtainMobile", "updatePassword", "responseWeixinRefundOrderNotify", "responseOrderPayNotify", "pageGoodsType", "pageImageText", "pageGoodsZoneType", "pageGoods", "findCurrentSeckillTime", "pageTeamGoods", "pageSeckillGoods", "detailGoods", "findGoodsPrice", "detailSellerShopByUserId", "findByBusinessIdAndTypeAndUser"};

    @Autowired
    private UserSessionManager userSessionManager;

    /*@Bean
    public SessionInterceptor authHandlerIntercept(){
        return new SessionInterceptor();
    }*/

    /*public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authHandlerIntercept()).addPathPatterns("/**");
    }*/
    //该方法用于添加拦截器。在配置好的拦截器中，会将自定义的SessionInterceptor拦截器添加到拦截器注册表中
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SessionInterceptor(this.userSessionManager, UN_VALIDATE_STR));
    }

    //该方法用于添加自定义异常解析器。会将自定义的HandlerExceptionResolverBean异常解析器添加到解析器列表中
    /*public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        exceptionResolvers.add(new HandlerExceptionResolverBean());
        super.extendHandlerExceptionResolvers(exceptionResolvers);
    }*/
    /*
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.registerModule(simpleModule);

        SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(smt);

        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, (JsonSerializer)new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, (JsonSerializer)new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, (JsonSerializer)new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        objectMapper.registerModule((Module)javaTimeModule);

        jackson2HttpMessageConverter.setObjectMapper(objectMapper);

        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON);
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setSupportedMediaTypes(list);
        converters.add(stringHttpMessageConverter);

        converters.add(jackson2HttpMessageConverter);
    }
    */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(new String[]{"/**"}).addResourceLocations(new String[]{"classpath:/static/"});
        registry.addResourceHandler(new String[]{"doc.html"
        }).addResourceLocations(new String[]{"classpath:/META-INF/resources/"});
        registry.addResourceHandler(new String[]{"/webjars/**"
        }).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"});
    }

   /* @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(Boolean.valueOf(true));
        config.addAllowedOrigin("*");//允许的原始请求源
        config.addAllowedHeader("*");//允许的请求头
        config.addAllowedMethod("*");//允许的请求方法
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }*/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")//设置允许的方法
                .allowCredentials(true)//是否允许证书 不再默认开启
                .maxAge(3600);//跨域允许时间
    }


}